package com.cskaoyan.controller.promote;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.promote.Coupon;
import com.cskaoyan.bean.promote.base.PromotePageData;
import com.cskaoyan.bean.promote.base.PromoteParam;
import com.cskaoyan.bean.promote.bo.CouponListSearchBO;
import com.cskaoyan.bean.promote.bo.CouponUserListSearchBO;
import com.cskaoyan.bean.promote.exception.*;
import com.cskaoyan.bean.promote.utils.PromoteValidationUtil;
import com.cskaoyan.service.promote.CouponService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author liXiaoFei
 * @date 2021/10/18 17:06
 */
@RestController
@RequestMapping("admin/coupon")
@Transactional
public class CouponController {

    @Autowired
    CouponService couponService;
    @RequiresPermissions("admin:ad:list")
    @GetMapping("list")
    public BaseRespVo list(@Validated PromoteParam promoteParam, CouponListSearchBO couponListSearchBO, BindingResult bindingResult) throws PromoteParamException {
        PromoteValidationUtil.valid(bindingResult);
        PromotePageData data = couponService.queryCouponList(promoteParam,couponListSearchBO);
        return BaseRespVo.ok(data);
    }
    @RequiresPermissions("admin:ad:create")
    @PostMapping("create")
    public BaseRespVo create(@RequestBody @Validated Coupon coupon, BindingResult bindingResult) throws PromoteParamException, AddCouponException {
        PromoteValidationUtil.valid(bindingResult);
        Coupon couponVO = couponService.addCoupon(coupon);
        return BaseRespVo.ok(couponVO);
    }
    @RequiresPermissions("admin:ad:delete")
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Coupon coupon) throws DeleteCouponException {
        couponService.deleteCoupon(coupon);
        return BaseRespVo.ok();
    }
    @RequiresPermissions("admin:ad:read")
    @GetMapping("read")
    public BaseRespVo read(Integer id) throws GetCouponException {
        Coupon coupon = couponService.getCouponById(id);
        return BaseRespVo.ok(coupon);
    }

    @RequiresPermissions("admin:ad:listuser")
    @GetMapping("listuser")
    public BaseRespVo listuser(@Validated CouponUserListSearchBO couponUserListSearchBO, PromoteParam promoteParam,BindingResult bindingResult) throws PromoteParamException {
        PromoteValidationUtil.valid(bindingResult);
        PromotePageData data = couponService.queryCouponUserList(promoteParam,couponUserListSearchBO);
        return BaseRespVo.ok(data);
    }
    @RequiresPermissions("admin:ad:update")
    @PostMapping("update")
    public BaseRespVo update(@RequestBody @Validated Coupon coupon, BindingResult bindingResult) throws PromoteParamException, UpdateCouponException {
        PromoteValidationUtil.valid(bindingResult);
        Coupon couponVO = couponService.updateCoupon(coupon);
        return BaseRespVo.ok(couponVO);
    }


}
